<template>
  <el-button size="small"  icon="Download" @click="exportExcel">导出</el-button>
</template>

<script setup>
import {reqExportExcel} from "@/api/index.js";

let props = defineProps({
  url: {
    type: String,
    required:  true
  },
  title:{
    type:String,
    default:"默认列表"
  }
})

let exportExcel = async () => {
  const res = await reqExportExcel(props.url)
  downloadExcel(res) // 处理文件下载
}
let downloadExcel = (res) => {
  // 1. 从响应头获取文件名（需后端设置 Content-Disposition）
  const fileName = decodeURIComponent(
      res.headers['content-disposition'].split('filename=')[1] || props.title
  )
  // 2. 创建 Blob 对象并生成下载链接
  const blob = new Blob([res.data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  })
  const url = window.URL.createObjectURL(blob)
  // 3. 创建 <a> 标签触发下载
  const link = document.createElement('a')
  link.href = url
  link.download = fileName
  document.body.appendChild(link)
  link.click()
  // 4. 清理资源
  window.URL.revokeObjectURL(url)
  document.body.removeChild(link)
}
</script>

<style scoped>

</style>
